var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    navList: [{
        name: '全部',
        _id: '0',
      },
      {
        name: '一年级',
        _id: '1',
      },
      {
        name: '二年级',
        _id: '2',
      },
      {
        name: '三年级',
        _id: '3',
      },
      {
        name: '四年级',
        _id: '4',
      },
      {
        name: '五年级',
        _id: '5',
      }, {
        name: '六年级',
        _id: '6',
    }],
   
    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '暂无相关书籍',
      text: '没有教师推荐的数据，可以看看家长推荐的优秀书籍',
      gurl: '../../pages/home/home',
      gtype: "switchTab"
    },
    page: 1,
    limit: 20,
    nj: "0",
    keyword:'', 
    totalpage:0,

    xturl: app.myUlife.ulifeUrl + "/",
    tjimagedefault: '/testimage/timg1.jpg',//默认教师推荐图片
    tjimageUrls: [],//教师推荐

    tjly:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */   
  onLoad: function (options) {
    this.setData({
      tjly: options.tjly
    })
    this.getdata();
  },

  getdata(){
    var that = this;
    var nj=that.data.nj;
    var tjly = that.data.tjly;
    var page = that.data.page;
    var totalpage = that.data.totalpage;
    var limit = that.data.limit;
    if(page!=1){
      if (page > totalpage) {
        return;
      }
    }
    wx.request({
      url: app.myUlife.ulifeUrl + 'whome/getJstjData.html',
      data: {
        nj: nj,
        tjly: tjly,
        page: page,
        limit: limit
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        //教师推荐
        var tjs = res.data.list;
        var totalpage = res.data.totalpage;
        var tjimageUrls = that.data.tjimageUrls;
        for (var i = 0; i < tjs.length;i++){
          tjimageUrls.push(tjs[i]);
        }
        that.setData({
          tjimageUrls: tjimageUrls,
          totalpage: totalpage,
          'prompt.hidden': tjimageUrls.length
        })
      }
    })
  },

  //输入关键字
  keyinput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  errormoretj: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var tjimageUrls = this.data.tjimageUrls    　　　　　　　//将图片列表数据绑定到变量
      tjimageUrls[errorImgIndex].FM = this.data.tjimagedefault //错误图片替换为默认图片
      this.setData({
        tjimageUrls: tjimageUrls
      })
    }
  },




  //上拉刷新
  tolower() {
    var that = this;
    var page = parseInt(that.data.page) + 1;
    that.setData({
      page: page
    });
    that.getdata();
  },

  //切换
  onTapTag(e) {
    var that = this;
    var nj = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    that.setData({
      activeIndex: index,
      nj: nj,
      tjimageUrls: [],
      page: 1,
      totalpage:0
    })
    that.getdata();
  },

  //查看书籍详情
  todetail(e) {
    var bid = e.currentTarget.dataset.bid; 
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid='+bid
    })
  },

  gotohxtjss() {
    var tjly = this.data.tjly;
    var keyword = this.data.keyword;
    wx.navigateTo({
      url: '../../pages/hxtjss/hxtjss?tjly=' + tjly + "&keyword=" + keyword
    })
  },



  todetailYxsj(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid=' + bid
    })
  },
})